package com.example.demo.utils;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class TCPClientReaderThread extends Thread{
    private Socket socket;

    public TCPClientReaderThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream is = socket.getInputStream();
            DataInputStream dis = new DataInputStream(is);//包装输入流
            while (true) {
                try {
                    String msg = dis.readUTF();//读取数据，连接断开进入异常
                    System.out.println(msg);
                } catch (IOException e) {
                    System.out.println("自己离线了");
                    break;
                }
            }
            dis.close();
            socket.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
